<template>
  <div class="app-container" style="padding: 20px">
    <el-form ref="form" label-width="80px">
      <el-form-item label="讲师姓名">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <!-- 讲师头像 -->
      <el-form-item label="讲师头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="path"
          class="avatar-uploader">
          <!-- 回显上传头像的标签 -->
          <img v-if="teacher.avatar" :src="teacher.avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker v-model="teacher.joinDate" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" :min="0" />
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
            -->
          <el-option :value="1" label="高级讲师" />
          <el-option :value="2" label="首席讲师" />
        </el-select>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro" />
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career" :rows="10" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">提交</el-button>
        <el-button @click="clear">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style scoped>
  .avatar-uploader .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar-uploader .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader img {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
import teacherApi from '@/api/teacher'
export default {
  data() {
    return {
      path: 'http://localhost:8120/admin/oss/file/upload?module=avatar',
      teacher: {
        sort: 0
      }
    }
  },
  created() {
    // console.log(this.$router) //获取路由对象
    // console.log(this.$route) // 当前页面的路由
    // console.log(this.$route.params.id)
    if (this.$route.params.id) {
      teacherApi.getTeacherById(this.$route.params.id).then(response => {
        this.teacher = response.data.item
      })
    }
  },
  methods: {
    // 取消
    clear() {
      this.teacher = {}
      this.teacher.sort = 0
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      console.log(res)
      this.teacher.avatar = res.data.path // 上传成功的图片地址
      // 强制刷新页面  渲染头像显示
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    saveOrUpdate() {
      if (!this.teacher.id) {
        // 如果是新增
        this.save()
      } else {
        // 如果是更新
        this.update()
      }
    },
    save() {
      teacherApi.teacherSave(this.teacher).then(response => {
        // this.$message 参考elementui文档 Message消息提示
        this.$message({
          type: 'success',
          message: response.message
        })
        // 路由跳转this.$router 参考 vue-router Api Router实例方法
        this.$router.push({ path: '/teacher/list' })
      })
    },
    update() {
      teacherApi.updateTeacher(this.teacher).then(response => {
        // this.$message 参考elementui文档 Message消息提示
        this.$message({
          type: 'success',
          message: response.message
        })
        // 路由跳转this.$router 参考 vue-router Api Router实例方法
        this.$router.push({ path: '/teacher/list' })
      })
    }
  }
}
</script>
